home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / sound / sndblst4.zip / WAVE.C < prev    next >
Text File  |  1993-12-11  |  4KB  |  158 lines

  1.  
  2. //------------------------------------------------------------------------------
  3. // Copyright (c) David Welch, 1993
  4. //------------------------------------------------------------------------------
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <conio.h>
  10. #include <dos.h>
  11.  
  12. #include "sb.h"
  13.  
  14. unsigned char ca;
  15. unsigned short ra;
  16. unsigned long la;
  17. FILE *fp;
  18. unsigned char gstring[80];
  19.  
  20. unsigned char major;
  21. unsigned char minor;
  22.  
  23. //.WAV stuff
  24. unsigned long rID;
  25. unsigned long rLen;
  26. unsigned long wID;
  27. unsigned long fID;
  28. unsigned long fLen;
  29. unsigned long fNext;
  30. unsigned short wFormatTag;
  31. unsigned short nChannels;
  32. unsigned short nSamplesPerSec;
  33. unsigned short nAvgBytesPerSec;
  34. unsigned long dID;
  35. unsigned long dLen;
  36. //------------------------------------------------------------------------------
  37. void main ( int argc, char *argv[] )
  38. {
  39.     if(argc==1)
  40.     {
  41.         printf(".WAV file not specified\n");
  42.         exit(1);
  43.     }
  44.     strcpy(gstring,argv[1]);
  45.     strcat(gstring,".WAV");
  46.     if((fp=fopen(gstring,"rb"))==0)
  47.     {
  48.         strcpy(gstring,argv[1]);
  49.         if((fp=fopen(gstring,"rb"))==0)
  50.         {
  51.             printf("Error opening .WAV file [%s]\n",argv[1]);
  52.             exit(1);
  53.         }
  54.     }
  55.     printf("FILE: [%s]\n",gstring);
  56.     sbinit();
  57.     dspwrite(0xE1);
  58.     major=dspread();
  59.     minor=dspread();
  60.     printf("DSP version %u.%u\n",major,minor);
  61.     if(major==3)
  62.     {
  63.         outportb(0x224,0x00); outportb(0x225,0xFF);
  64.         outportb(0x224,0x04); outportb(0x225,0xFF);
  65.         outportb(0x224,0x0A); outportb(0x225,0x00);
  66.         outportb(0x224,0x0C); outportb(0x225,0x26);
  67.         outportb(0x224,0x0E); outportb(0x225,0x20);
  68.         outportb(0x224,0x22); outportb(0x225,0x99);
  69.         outportb(0x224,0x26); outportb(0x225,0x00);
  70.         outportb(0x224,0x28); outportb(0x225,0x00);
  71.         outportb(0x224,0x2E); outportb(0x225,0x00);
  72.     }
  73.     sbmalloc();
  74.     fread(&rID,1,4,fp);
  75.     if(rID!=0x46464952)
  76.     {
  77.         printf("Not a RIFF format file\n");
  78.         exit(1);
  79.     }
  80.     fread(&rLen,1,4,fp);
  81.     printf("rLen = %lu\n",rLen);
  82.     fread(&wID,1,4,fp);
  83.     if(wID!=0x45564157)
  84.     {
  85.         printf("Not a WAVE format chunk\n");
  86.         exit(1);
  87.     }
  88.     fread(&fID,1,4,fp);
  89.     if(fID!=0x20746D66)
  90.     {
  91.         printf("Not a fmt WAVE Format Chunk\n");
  92.         exit(1);
  93.     }
  94.     fread(&fLen,1,4,fp);
  95.     fNext=fLen+ftell(fp);
  96.     printf("fLen %lu\n",fLen);
  97.     printf("fNext %lu\n",fNext);
  98.     fread(&wFormatTag,1,2,fp);
  99.     if(wFormatTag!=1)
  100.     {
  101.         printf("Data is not PCM\n");
  102.         exit(1);
  103.     }
  104.     fread(&nChannels,1,2,fp);
  105.     if((nChannels!=1)&&(nChannels!=2))
  106.     {
  107.         printf("Must be MONO or STEREO data\n");
  108.         exit(1);
  109.     }
  110.     if(nChannels==2)
  111.     {
  112.         printf("STEREO data will be half speed (twice as much data played mono)\n");
  113.     }
  114.     printf("nChannels %u\n",nChannels);
  115.     fread(&nSamplesPerSec,1,2,fp);
  116.     printf("nSamplesPerSec %u\n",nSamplesPerSec);
  117.     fread(&nAvgBytesPerSec,1,2,fp);
  118.     printf("nAvgBytesPerSec %u\n",nAvgBytesPerSec);
  119.     fseek(fp,fNext,0);
  120.     fread(&dID,1,4,fp);
  121.     if(dID!=0x61746164)
  122.     {
  123.         printf("Not a DATA chunk\n");
  124.         exit(1);
  125.     }
  126.     fread(&dLen,1,4,fp);
  127.     printf("dLen %lu\n",dLen);
  128.     ca=256UL-(1000000UL/nSamplesPerSec);
  129.     printf("Time Constant %u\n",ca);
  130.     sbsettc(ca);
  131.     spkon();
  132.     while(dLen)
  133.     {
  134.         if(dLen>65000) la=65000; else la=dLen;
  135.         dLen-=la;
  136.         fread(aligned,1,la,fp);
  137.         sbplay(la);
  138.         printf("%lu Samples\n",la);
  139. //      while(dmacount()!=0xFFFF)
  140.         dmastatus();
  141.         while(!dmastatus())
  142.         {
  143.             if(kbhit())
  144.             {
  145.                 sbhaltdma();
  146.                 getch();
  147.                 dLen=0;
  148.                 break;
  149.             }
  150.         }
  151.     }
  152.     spkoff();
  153. }
  154. //------------------------------------------------------------------------------
  155. // Copyright (c) David Welch, 1993
  156. //------------------------------------------------------------------------------
  157.  
  158.